package com.example.controller;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.example.entity.Goods;
import com.example.entity.Orders;
import com.example.uitl.PayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Map;

@Controller
@RequestMapping("/api/alipay")
public class AliPayController {
    @Autowired
    private PayUtil payUtil;
//    @Autowired
//    private OrderService orderService;
//
//    private OrderModel orderModels = null;
    private String tokens = "";
//    @Autowired
//    private JwtHelper jwtHelper;

    @RequestMapping("/order")
    public String test()  {
        return "order";
    }

    @RequestMapping("/orderlist")
    public String orderlist()  {
        return "orderlist";
    }

    @RequestMapping("/pay")
    public void alipay(int oid,BigDecimal paymoney,String name, HttpServletResponse response) throws AlipayApiException, IOException {
//        BigDecimal paymoney = orders.getPaymoney(); // 假设 paymoney 是 BigDecimal
//        String paymoneyStr = paymoney.toString(); // 转换为 String
        // 获取支付宝返回的HTML表单
        String html = payUtil.sendRequestToAlipay(
                String.valueOf(oid),
                Float.valueOf(String.valueOf(paymoney)),
                String.valueOf(name)
        );
        // 设置响应类型为HTML，直接输出内容
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().write(html); // 关键！直接输出HTML
        response.getWriter().flush();
    }


    //    支付宝异步通知
    @RequestMapping("/notify")
    public String success(String out_trade_no)  {
        System.out.println("支付宝异步通知");
        System.out.println("订单号："+out_trade_no);
        //根据订单查询订单信息，更改订单状态
        //....其他业务逻辑
        // 最后支付宝要求返回success，这个不是success页面
        return "success";
    }

    //  同步通知，当我们支付完成之后跳转这个请求并携带参数，我们将里面的订单id接收到，通过订单id查询订单信息，信息包括支付是否成功等
    @GetMapping("/return")
    public String returns(String out_trade_no) throws ParseException {
        System.out.println("同步通知");
        System.out.println("订单号：" + out_trade_no);
        String query = payUtil.query(out_trade_no);
        System.out.println("==>" + query);
        JSONObject jsonObject = JSONObject.parseObject(query);
        Object o = jsonObject.get("alipay_trade_query_response");
        Map map = (Map) o;
        System.out.println(map);
        Object tradeStatus = map.get("trade_status");
        if (tradeStatus.equals("TRADE_SUCCESS")) {
            System.out.println("订单支付成功");
            System.out.println(tradeStatus);
            return "student/orderslist";
        } else {
//            支付失败要执行的操作
            System.out.println("订单支付失败");
            return "index";
        }
    }


}